package com.j48.entrancegurad.service.impl;

import com.j48.entrancegurad.mapper.PowerMapper;
import com.j48.entrancegurad.model.P_C;
import com.j48.entrancegurad.model.Power;
import com.j48.entrancegurad.service.IPowerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

@Service
public class PowerService implements IPowerService {

    @Autowired
    private PowerMapper powerMapper;

    /**
     * 查询全部权限
     * @param p_cs 权限编号列表
     * @return
     */
    @Override
    public ArrayList<Power> select(ArrayList<P_C> p_cs) {
        ArrayList<Power> powers=new ArrayList<Power>();
        for (int i=0;i<p_cs.size();i++) {
            Power power=new Power();
            power.setPower_id(p_cs.get(i).getP_c_power_id());
            power=powerMapper.selectById(power);
            powers.add(power);
        }
        return powers;
    }

    /**
     * 查询父级权限
     * @param powers 角色所拥有的权限
     * @return
     */
    @Override
    public HashMap<Integer,Power> MainMenu(ArrayList<Power> powers){
        HashMap<Integer,Power> powers1=new HashMap();
        for (int i=0;i<powers.size();i++){
            Power power=powerMapper.selectParentMenu(powers.get(i));
            int key=power.getPower_id();
            powers1.put(key,power);
        }
        return powers1;
    }

    /**
     * 查询子级权限
     * @param powers
     * @param power
     * @return
     */
    @Override
    public ArrayList<Power> ChildMenu(ArrayList<Power> powers,Power power){
        ArrayList<Power> powers1=new ArrayList<Power>();
        for(int i=0;i< powers.size();i++){
            Power power1=powerMapper.selectLowerMenu(powers.get(i));
            if(power1!=null) {
                powers1.add(power1);
            }
        }
        ArrayList<Power> powers2 = powerMapper.selectChildrenMenu(power);
        ArrayList<Power> powers3=new ArrayList<Power>();
        for(int i=0;i< powers1.size();i++){
            for (int j=0;j<powers2.size();i++) {
                if(powers1.get(i).equals(powers2.get(j))){
                    powers3.add(powers1.get(i));
                }
            }
        }
        return powers3;
    }

    /**
     * 查询权限
     * @param power
     * @return 对象
     */
    public Power selectpower(Power power){
        return powerMapper.selectByName(power);
    }

    /**
     * 通过ID查power
     * @param power
     * @return
     */
    public ArrayList<Power> selectByID(Power power){
       return powerMapper.selectBypowerId(power);
    }
}
